﻿Option Strict Off

Imports Runa.Common.Helper
Imports Runa.Foundation.IO
Imports System.Reflection
Imports System.Text.RegularExpressions

#If WINDOWS Or XBOX Then
Imports System.IO.IsolatedStorage
Imports System.IO

#If WINDOWS Then
Imports System.Windows.Forms
#End If

Module Program

    ''' <summary>
    ''' The main entry point for the application.
    ''' </summary>
    Public Sub Main(ByVal ParamArray args As String())
        Try
#If WINDOWS Then

            Debuger = New DebugAssistant.LibDebug()
            g_fileStorage = New FileSystemGameDirectory(Path.Combine(My.Application.Info.DirectoryPath, "DownloadedGames"))
            'g_fileStorage = New IsolatedFileStorageGameDirectory("", fileStorage)

            'Dim di As New DirectoryInfo(My.Application.Info.DirectoryPath + "\Downloadedgames")
            'For Each fi In di.GetFiles("*.rpk")
            '    Using isolatedFileStream = FileStorage.OpenFile(Path.ChangeExtension(fi.Name, ".zip"), IO.FileMode.OpenOrCreate)
            '        Using fileWriter As New BinaryWriter(isolatedFileStream)
            '            fileWriter.Write(My.Computer.FileSystem.ReadAllBytes(fi.FullName))
            '            fileWriter.Close()
            '        End Using
            '        isolatedFileStream.Close()
            '    End Using
            '    fi.Delete()
            'Next

            Dim di As New DirectoryInfo(My.Application.Info.DirectoryPath)
            For Each fi In di.GetFiles("*.zip")
                ExtractFromStream(fi.OpenRead(), IO.Path.Combine(di.FullName, IO.Path.GetFileNameWithoutExtension(fi.Name)))
                'fi.Delete()
            Next

            'If args IsNot Nothing AndAlso args.Length > 0 Then
            '    Dim filePath As String = ""
            '    For i As Integer = 0 To args.Length - 1
            '        filePath += " " + args(i)
            '    Next
            '    MessageBox.Show(filePath)
            'End If

            g_externalFiles = GetCommandLineArgs(args)

#Else
        fileStorage = New IsolatedFileStorageGameDirectory()

#If Not Debug Then
        If fileStorage.FileExists("Sample.zip") = False Then
#Else
        If fileStorage.DirectoryExists("Sample") Then
            fileStorage.DeleteDirectory("Sample", True)
        End If
#End If

#If WINDOWS Then
        Using isolatedFileStream = fileStorage.OpenFile("Sample.zip", FileMode.OpenOrCreate)
            Using fileWriter As New BinaryWriter(isolatedFileStream)
                fileWriter.Write(My.Computer.FileSystem.ReadAllBytes("DownloadedGames\Sample.zip"))
                fileWriter.Close()
            End Using
            isolatedFileStream.Close()
        End Using
#End If

#If Not Debug Then
        End If
#End If

#If Not Windows Then
        Dim gameName As String = "Sample"

        Using game As New GalgamePlayerForWindows(gameName)
            game.Run()
        End Using
#End If
#End If
        Catch ex As Exception
            Debug.Assert(False, ex.Message & vbCrLf & ex.StackTrace.ToString())
        End Try
    End Sub

End Module

#End If